//
//  HistoryViewController.swift
//  UUCartoon
//
//  Created by Galaxy on 2021/6/29.
//  Copyright © 2021 qykj. All rights reserved.
//

import UIKit
import UICollectionViewLeftAlignedLayout
class HistoryViewController: BaseViewController {
    var listArr:[CartoonModel] = []{
        didSet{
            mainTable.listArr = listArr
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        title = "历史记录"
        navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "清空", style: .plain, target: self, action: #selector(cleanHistory))
        getHistoryData()
    }
    
    @objc func cleanHistory(){
        Tool.makeAlertController(title: "警告", message: "是否删除所有历史记录") {
            if SqlTool.init().cleanHistory() {
                self.listArr = []
            }
        }
    }
    
    func getHistoryData(){
        listArr = SqlTool.init().getHistory()
    }
    lazy var mainTable: UUTableView = {
        let tableView = UUTableView.init(frame: .zero, style: .plain)
        view.addSubview(tableView)
        tableView.estimatedRowHeight = 150
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        tableView.register(UINib.init(nibName: "ComicTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "cell")
        tableView.cellForRow {[weak self] (indexPath) in
            let model = self!.listArr[indexPath.row]
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! ComicTableViewCell
            cell.model = model
            cell.rankViewConstraintWidth.constant = 40
            return cell
        }
        tableView.cellItemDidSelected {[weak self] (indexPath) in
            let model = self!.listArr[indexPath.row]
            let VC = ChapterViewController.init()
            VC.type = model.type
            VC.detailUrl = model.detailUrl
            VC.cartoon_id = model.cartoon_id
            self?.navigationController?.pushViewController(VC, animated: true)
        }
        tableView.emptyDataSetView { emptyView in
            emptyView.detailLabelString(NSAttributedString.init(string: "暂时没有历史记录")).image(UIImage.init(named: "empty_img"))
        }
        return tableView
    }()
}
